--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 消息的处理

local skynet = require "skynet"
local log = require "Log"
local svrName = 'ps_service'

local ps_robotMgr = class("ps_robotMgr")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function ps_robotMgr:ctor(message, robotMgr)
	print("---------------------ps_robotMgr ctor")
	self.robotMgr = robotMgr	
	self.msg = message	
	self.tbService = {}
	self.tableRobots = {}
	self.tbCount = {}
	self.gameid = 10
	self:register()
end

--注册本服务里的消息
function ps_robotMgr:register()
	self.msg:registerSelf('add_robot',handler(self,self.addRobot))
	self.msg:registerSelf('del_robot',handler(self,self.delRobot))

	self.msg:registerSelf('player_enter_table_res',handler(self,self.player_enter_table_res))
	self.msg:registerSelf('player_leave_table_res',handler(self,self.player_leave_table_res))
	self.msg:registerSelf('nt_settle',handler(self,self.nt_settle))
	--
end

function ps_robotMgr:addRobot(data,user)
	print("____addRobot_____pinshi__",data, user)
	for k, v in pairs(user) do
		local playerid = v.uid
		if self.tbService[playerid] then 
			self:delRobot(data)
		end

		local sql = string.format("select * from common_room where sub_id = %d", data.roomid)
		local res = skynet.call("mysql_service", "lua", "execute", sql)
		local room_config = res[1]

		local sql = string.format("select game from userinfo where uid=%d;", playerid);
		local res = skynet.call("mysql_service", "lua", "execute", sql)
		data.game = 0
		if res and res[1] and res[1].game then
			data.game = res[1].game 
		end

		local sql = string.format("select * from usergame where uid = %d", playerid)
		local res = skynet.call("mysql_service", "lua", "execute", sql)
		if table.empty(res) then
			break
		end
		local usergame = res [1]
		print(" ------------------->room_config.min_money", usergame.uchip,room_config.min_money)
		if usergame.uchip < room_config.min_money then	
			local data =  {money = math.random(room_config.min_money, room_config.min_money * 2), uid = playerid, game = data.game, 
							gameid = self.gameid, code = 0, gametype = data.roomid}
			skynet.call("manager", "lua", "change_money", data)
		elseif usergame.uchip > room_config.max_money and room_config.max_money ~= 0 then
			local changeMoney = usergame.uchip - room_config.max_money *math.random(50, 90) /100
			local data =  {money = -1 * changeMoney , uid = playerid, game = data.game, 
							gameid = self.gameid, code = 0, gametype = data.roomid}
			skynet.call("manager", "lua", "change_money", data)
		end

		local service = skynet.newservice(svrName)
		local param = {playerid = playerid, roomid = data.roomid, 
					gameid = data.gameid, tableid = data.tableid}
		skynet.call(service, 'lua', 'start', param)	
		self.tbService[playerid] = service
	end
end

function ps_robotMgr:delRobot(data)
	local playerid = data.mid
	local service = self.tbService[playerid]
	if service then
		skynet.send(service, 'lua', 'exit')
		self.tbService[playerid] = nil
	end
end

function ps_robotMgr:player_enter_table_res(data)
	self.tableRobots[data.tableid] = self.tableRobots[data.tableid] or {}
	table.insert(self.tableRobots[data.tableid], data.playerid)
end

function ps_robotMgr:player_leave_table_res(data)
	if self.tableRobots[data.tableid] then
		for k, v in ipairs(self.tableRobots[data.tableid] ) do
			if v == data.playerid then
				table.remove(self.tableRobots[data.tableid], k)
				print("------------------------------->player_leave_table_res2")
				self:delRobot({mid = v})
				break
			end			
		end
	end
end

function ps_robotMgr:isRobet(tableid, playerid)
	local tbPlayerid = self.tableRobots[tableid]
	if not table.empty(tbPlayerid) then
		for k, v in pairs(tbPlayerid) do
			if v == playerid then
				return true
			end
		end
	end
	return false
end


return ps_robotMgr